package com.game.view;

import com.game.config.Coefficient;
import com.game.controller.*;
import com.game.data.BagData;
import com.game.data.MapData;
import com.game.data.RoleData;
import com.game.model.api.*;
import com.game.model.equip.Equip;
import com.game.model.equip.PlayerEquip;
import com.game.model.goods.Goods;
import com.game.model.map.GameMap;
import com.game.model.map.MapPoint;
import com.game.model.map.Tranfer;
import com.game.model.role.Monster;
import com.game.model.role.NPC;
import com.game.model.role.PlayerRole;
import com.game.model.role.Role;
import com.game.model.task.Task;
import com.game.utils.Format;
import com.game.utils.MyScaner;
import com.game.utils.Storage;

import java.util.Iterator;
import java.util.Map;

/**
 * @Description:打印界面
 **/
public class Print {
    //主界面
    public static void printMain() {
        Screen.clean();
        Message.printMsg();
        System.out.println("----------主页面----------");
        System.out.print("【0.角色】");
        System.out.print("【1.装备】");
        System.out.print("【3.地图】");
        System.out.println();
        System.out.print("【4.背包】");
        System.out.print("【5.活动】");
        System.out.print("【6.训练】");
        System.out.println();
        System.out.print("【7.任务】");
        System.out.print("【8.商店】");
        System.out.print("【9.系统】");
        System.out.println();
        System.out.println("-------------------------");
        System.out.print("请选择:");
        String key = MyScaner.getKeyDown();
        switch (key) {
            case "0":
                printRole();
                break;
            case "1":
                printEquip();
                break;
            case "3":
                printGameMap(MapData.gameMap);
                break;
            case "4":
                printBag(1);
                break;
            case "7":
                printTask();
                break;
            case "9":
                printSetting();
                break;
            default:
                printMain();
                break;
        }
    }

    //角色信息
    public static void printRole() {
        Screen.clean();
        Message.printMsg();
        //获取计算后的属性
        Role role = RoleController.getRole(RoleData.playerRole);
        System.out.println("------基本属性------");
        if (role instanceof PlayerRole) {
            System.out.println("职业:" + TranslateToChinese.getRoleType(((PlayerRole) role).getRoleType()));
        }
        System.out.println("等级:" + role.getLevel());
        System.out.println("血量:" + role.getBaseProperty().getHP());
        System.out.println("怒气:" + role.getBaseProperty().getMp());
        System.out.println("攻击力:" + role.getBaseProperty().getATK());
        System.out.println("防御力:" + role.getBaseProperty().getDEF());
        System.out.println("命中:" + role.getSpecialProperty().getHIT());
        System.out.println("暴击:" + role.getSpecialProperty().getCRI());
        System.out.println("闪避:" + role.getSpecialProperty().getAVO());
        System.out.println("韧性:" + role.getSpecialProperty().getCHR());
        if (role instanceof PlayerRole) {
            double cof = Math.pow(1 + Coefficient.EXPLEVEL, role.getLevel());
            long neddExp = (long) (Coefficient.EXPINIT * cof);
            System.out.println("经验:" + ((PlayerRole) role).getExp() + "/" + neddExp);
        }

        System.out.println("------装备-------");

        Iterator<Map.Entry<EquipType, Equip>> iterator = RoleData.equipMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<EquipType, Equip> next = iterator.next();
            PlayerEquip playerEquip = (PlayerEquip) next.getValue();
            if (playerEquip != null) {
                System.out.println(TranslateToChinese.getEquipType(playerEquip.getEquipType()) + ":" + playerEquip.getName());
            }
        }
        System.out.print("【0.主界面】");
        System.out.print("【1.升级】");
        System.out.println();
        System.out.print("请选择:");
        String key = MyScaner.getKeyDown();
        switch (key) {
            case "0":
                printMain();
                break;
            case "1":
                RoleController.upPlayerRole();
                printRole();
                break;
            default:
                printRole();
                break;
        }
    }

    //装备信息
    public static void printEquip() {
        Screen.clean();
        Message.printMsg();
        if (RoleData.equipMap==null)return;
        Iterator<Map.Entry<EquipType, Equip>> iterator = RoleData.equipMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<EquipType, Equip> next = iterator.next();
            System.out.println("------------------------------------【"+Format.getEquipType(next.getKey())+"】------------------------------------");
            if (next.getValue()!=null){
                System.out.println("【名称】"+next.getValue().getName());
                System.out.println("【品质】"+Format.getQuality(next.getValue().getQuality()));
                System.out.println("【等级】"+next.getValue().getLevel());
                System.out.println("【属性】"+next.getValue().getBaseProperty().printInfo()+next.getValue().getSpecialProperty().printInfo());
                System.out.println("【描述】"+next.getValue().getDescription());
            }else {
                System.out.println("未装备");
            }
        }
        System.out.println("【0.返回】");
        System.out.print("你的选择:");
        String keyDown = MyScaner.getKeyDown();
        switch (keyDown){
            case "0":
                printMain();
                break;
            default:printMain();
        }
    }

    //打印地图
    public static void printGameMap(GameMap gameMap) {
        Screen.clean();
        Message.printMsg();
        System.out.println("-------------------------------------" + gameMap.getName() + "------------------------------------");
        MapPoint[][] mapPoints = gameMap.getMapPoints();
        for (int i = 0; i < mapPoints.length; i++) {
            for (int j = 0; j < mapPoints[i].length; j++) {
                //如果地图上点里没有东西就显示[x,y]
                if (mapPoints[i][j].getPointObject() == null || mapPoints[i][j].getPointObject().size() <= 0) {
                    System.out.print("[" + gameMap.getMapPoints()[i][j].getX() + "," + gameMap.getMapPoints()[i][j].getY() + "]\t");
                }
                //当位置点只有一个物体时
                else if (mapPoints[i][j].getPointObject().size() == 1) {
                    Object o = mapPoints[i][j].getPointObject().get(0);
                    //如果存在当前角色,打印“自己”
                    if (o instanceof PlayerRole) {
                        System.out.print(TranslateToChinese.getFormatLogString("[自己]", 32, 0, 1) + "\t");
                    }
                    //如果存在怪物，打印怪物名字
                    if (o instanceof Monster) {
                        System.out.print(TranslateToChinese.getFormatLogString("[" + ((Monster) o).getName() + "]", 34, 0, 1));
                    }
                    //如果是NPC
                    if (o instanceof NPC) {
                        System.out.print(Format.colorFormat("[" + ((NPC) o).getName() + "]", Color.YELLOW));
                    }
                    //如果是传送阵
                    if (o instanceof Tranfer) {
                        System.out.print(TranslateToChinese.getFormatLogString("[" + MapData.getGameMapById(((Tranfer) o).getNextId()).getName() + "]", 34, 0, 1));
                    }
                }//当位置有多个物体
                else {
                    String msg = "";
                    for (Object o : mapPoints[i][j].getPointObject()) {
                        //如果存在当前角色，就打印“自己”
                        if (o instanceof PlayerRole) {
                            msg = TranslateToChinese.getFormatLogString("[自己]", 32, 0, 1) + "\t";
                            break;
                        }//其他就打印“”
                        else {
                            msg = TranslateToChinese.getFormatLogString("[物体X" + mapPoints[i][j].getPointObject().size() + "]", 34, 0, 1);
                        }
                    }
                    System.out.print(msg);
                }
            }
            System.out.println();
        }
        System.out.println("-------------------------------------" + gameMap.getName() + "------------------------------------");
        //打印当前角色位置点的信息
        MapPoint roleInMap = MapController.getRoleInMap(gameMap);
        System.out.println("当前位置[" + roleInMap.getX() + "," + roleInMap.getY() + "]:");
        if (roleInMap.getPointObject().size() <= 1) {
            System.out.println("这里除了自己,空空如也...");
        } else {
            for (Object o : roleInMap.getPointObject()) {
                //如果是怪物，可以攻击
                if (o instanceof Monster) {
                    System.out.print("【攻击.ATK" + ((Monster) o).getId() + "】");
                    System.out.println(((Monster) o).printInfo());
                }
                //如果是NPC
                if (o instanceof NPC) {
                    System.out.print("【交谈.t" + ((NPC) o).getId() + "】【" + ((NPC) o).getName() + "." + ((NPC) o).getLevel() + "LV】" + ((NPC) o).getDescription());
                    System.out.println();
                }
                //如果是传送阵，显示是否传送
                if (o instanceof Tranfer) {
                    System.out.println("前往："+MapData.getGameMapById(((Tranfer) o).getNextId()).getName());
                    System.out.println("【描述】"+((Tranfer) o).getDescription());
                    System.out.println("【6.传送】");
                }
            }

        }
        System.out.println("---------------------------------------------------------------------------------");
        System.out.println("行走:【A】【W】【S】【D】 【0.主界面】");
        System.out.print("请选择:");
        String keyDown = MyScaner.getKeyDown();
        for (Object o : roleInMap.getPointObject()) {
            //攻击怪物
            if (o instanceof Monster) {
                if (keyDown.equalsIgnoreCase("ATK" + ((Monster) o).getId())) {
                    //攻击
                    new FightController(RoleController.getRole(RoleData.playerRole), (Monster) o).fight();
                    return;
                }
            }
            //NPC交谈
            if (o instanceof NPC) {
                if (keyDown.equalsIgnoreCase("T" + ((NPC) o).getId())) {
                    printTalk((NPC) o);
                    return;
                }
            }
            //传送
            if (o instanceof Tranfer) {
                if ("6".equalsIgnoreCase(keyDown)) {
                    //删除当前地图角色位置
                    MapPoint roleInMap1 = MapController.getRoleInMap(MapData.gameMap);
                    MapController.removeObjectInMap(MapData.gameMap, roleInMap1.getX(), roleInMap1.getY(), RoleData.playerRole);
                    //获取下一个地图
                    MapData.gameMap = MapData.getGameMapById(((Tranfer) o).getNextId());
                    //将角色添加到下一个地图
                    MapController.addObjectInMap(MapData.gameMap, ((Tranfer) o).getMapPoint().getX(), ((Tranfer) o).getMapPoint().getY(), RoleData.playerRole);
                    printGameMap(MapData.gameMap);
                    return;
                }
            }
        }
        //行走
        switch (keyDown) {
            case "0":
                printMain();
                break;
            case "A":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.LEFT, RoleData.playerRole));
                break;
            case "a":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.LEFT, RoleData.playerRole));
                break;
            case "S":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.DOWN, RoleData.playerRole));
                break;
            case "s":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.DOWN, RoleData.playerRole));
                break;
            case "D":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.RIGHT, RoleData.playerRole));
                break;
            case "d":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.RIGHT, RoleData.playerRole));
                break;
            case "W":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.UP, RoleData.playerRole));
                break;
            case "w":
                printGameMap(RoleController.moveTo(MapData.gameMap, Move.UP, RoleData.playerRole));
                break;
            default:
                Message.error("格式错误！！！");
                printGameMap(MapData.gameMap);
                break;
        }
    }

    //打印任务页面
    public static void printTask(){
        Screen.clean();
        Message.printMsg();
        System.out.println("----------------------------------任务------------------------------------");
        if (RoleData.taskMap==null||RoleData.taskMap.size()<=0){
            System.out.println("暂无接取任务");
        }else {
            Iterator<Map.Entry<Integer, Task>> iterator = RoleData.taskMap.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Integer, Task> next = iterator.next();
                System.out.println("【任务名称】"+next.getValue().getName());
                System.out.println("【任务描述】"+next.getValue().getDecription());
                System.out.println("【任务要求】"+TaskController.printTaskNeed(next.getValue()));
                System.out.println("【任务奖励】"+TaskController.printTaskRewards(next.getValue()));
                String state = "";
                if (TaskController.isGet(next.getValue())){
                    if (TaskController.isDone(next.getValue())){
                        state = Format.colorFormat("(可提交!!!)",Color.GREEN)+"【s"+next.getValue().getId()+".提交】";
                    }else {
                        state = Format.colorFormat("(未完成)",Color.YELLOW);
                    }
                }
                System.out.println("【任务状态】"+state);
                System.out.println("-------------------------------------------------------------------------");
            }
        }
        System.out.println("【0.返回】");
        System.out.print("你的选择:");
        String keyDown = MyScaner.getKeyDown();
        Iterator<Map.Entry<Integer, Task>> iterator1 = RoleData.taskMap.entrySet().iterator();
        while (iterator1.hasNext()){
            Map.Entry<Integer, Task> next = iterator1.next();
            //提交任务
            if (keyDown.equalsIgnoreCase("S"+next.getValue().getId())){
                boolean submit = TaskController.submit(next.getValue());
                if (submit){
                    Message.success("提交成功！！！");
                }else {
                    Message.error("提交错误！！！");
                }
                printTask();
                return;
            }
        }
        switch (keyDown){
            case "0":printMain();break;
            default:printMain();
        }
    }
    //打印背包
    public static void printBag(int check) {
        Screen.clean();
        Message.printMsg();
        System.out.println("---------------------------------我的背包--------------------------------------");
        int countFlag = 0;
        Map<Goods, Integer> goodsMap = BagData.bag.getGoodsMap();
        int bagSize = BagData.bag.getBagSize();
        Goods currentGoods = null;
        int currentGoodsCount = 0;
        Iterator<Map.Entry<Goods, Integer>> iterator = goodsMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Goods, Integer> next = iterator.next();
            if (++countFlag % 5 == 0) {
                System.out.println();
            }
            //当前光标位置的物品
            if (countFlag == check) {
                System.out.print(Format.underLineFormat("(" + countFlag + ")【" + next.getKey().getName() + "】X" + next.getValue(), 31) + "\t");
                currentGoods = next.getKey();
                currentGoodsCount = next.getValue();
                continue;
            }
            System.out.print("(" + countFlag + ")【" + next.getKey().getName() + "】X" + next.getValue() + "\t");
        }
        System.out.println();
        System.out.println("【背包容量:" + goodsMap.size() + "/" + BagData.bag.getBagSize() + "】");
        System.out.println("---------------------------------我的背包--------------------------------------");
        if (goodsMap.size() <= 0) {
            System.out.println("空空如也");
            System.out.println("【0.返回主界面】");
        } else {
            System.out.println("当前位置物品:");
            System.out.print("【名称】" + currentGoods.getName() + "\t");
            System.out.print("【类型】" + Format.getGoodsType(currentGoods.getType()) + "\t");
            System.out.print("【品质】" + Format.getQuality(currentGoods.getQuality()) + "\t");
            System.out.print("【使用等级】" + currentGoods.getUseLevel() + "\t");
            if (currentGoods instanceof Equip) {
                if (currentGoods instanceof PlayerEquip)
                    System.out.print("【位置】" + ((PlayerEquip) currentGoods).getEquipType() + "\t");
                System.out.println();
                System.out.print("【属性】");
                System.out.println(((Equip) currentGoods).getBaseProperty().printInfo() + ((Equip) currentGoods).getSpecialProperty().printInfo());
            } else {
                System.out.println();
            }
            System.out.println("【描述】*" + currentGoods.getDescription()+ "*");
            System.out.println("【数量】" + currentGoodsCount);
            System.out.println();
            System.out.println("【1.使用】【2.查看其他物品】【0.返回主界面】");
        }
        System.out.print("你的选择:");
        String keyDown = MyScaner.getKeyDown();
        switch (keyDown) {
            case "1":
                System.out.print("使用数量:");
                String keyDown1 = MyScaner.getKeyDown();
                try {
                    int i = Integer.parseInt(keyDown1);
                    if (i <= 0 || i > currentGoodsCount) {
                        Message.error("请输入正确数量！！！");
                        printBag(check);
                        break;
                    }
                    GoodsController.useGoods(currentGoods, i);
                    printBag(check);
                } catch (Exception e) {
                    Message.error("输入格式错误！！！");
                    printBag(check);
                    break;
                }
                break;
            case "0":
                printMain();
            case "2":
                System.out.print("请选择背包格子位置(第3格就输入3)");
                String keyDown2 = MyScaner.getKeyDown();
                try {
                    int i = Integer.parseInt(keyDown2);
                    if (i <= 0 || i > goodsMap.size()) {
                        Message.error("该位置没有物品！！！");
                        printBag(check);
                        break;
                    }
                    printBag(i);
                } catch (Exception e) {
                    Message.error("输入格式错误！！！");
                    printBag(check);
                    break;
                }
            default:
                printMain();
        }
    }

    //返回地图按钮
    public static void back() {
        System.out.println("【0.返回地图】【1.返回主界面】");
        System.out.print("你的选择:");
        String keyDown = MyScaner.getKeyDown();
        switch (keyDown) {
            case "0":
                printGameMap(MapData.gameMap);
                break;
            case "1":
                printMain();
            default:
                printMain();
        }
    }

    //任务描述
    public static void printTask(NPC npc, Task task) {
        Screen.clean();
        Message.printMsg();
        //任务描述
        System.out.println("任务描述："+task.getDecription());
        //任务需求
        System.out.println("任务要求："+TaskController.printTaskNeed(task));
        //任务奖励
        System.out.println("任务奖励："+TaskController.printTaskRewards(task));
        //如果任务还没接取就显示接取
        if (!TaskController.isGet(task)) {
            System.out.print(Format.colorFormat("!【1.接取任务】", Color.YELLOW));
            System.out.println("【0.返回】");
            System.out.print("你的选择:");
            String keyDown = MyScaner.getKeyDown();
            switch (keyDown) {
                case "1":
                    //接取任务
                    if (TaskController.getTask(task)) {
                        Message.success("任务接取成功！！！");
                        printTask(npc, task);
                    } else {
                        Message.error("任务接取功能异常");
                        printTalk(npc);
                    }
                    break;
                case "0":
                    printTalk(npc);
                    break;
                default:
                    printTask(npc, task);
            }
        }
        //如果接了就显示提交任务或者未完成
        if (TaskController.isDone(task)) {
            System.out.print(Format.colorFormat("!【1.提交任务】", Color.GREEN));
            System.out.println("【0.返回】");
            System.out.print("你的选择:");
            String keyDown = MyScaner.getKeyDown();
            switch (keyDown) {
                case "1":
                    //提交任务
                    boolean submit = TaskController.submit(task);
                    if (submit) {
                        Message.success("任务完成！！！");
                        printTalk(npc);
                    } else {
                        Message.error("任务功能提交异常");
                        printTalk(npc);
                    }
                    break;
                case "0":
                    printTalk(npc);
                    break;
                default:
                    printTask(npc, task);
            }
        } else {
            System.out.print(Format.colorFormat("【未完成】", Color.ORGANGE));
            System.out.println("【0.返回】");
            System.out.print("你的选择:");
            String keyDown = MyScaner.getKeyDown();
            switch (keyDown) {
                case "0":
                    printTalk(npc);
                    break;
                default:
                    printTask(npc, task);
            }
        }
    }

    //NPC谈话界面
    public static void printTalk(NPC npc) {
        Screen.clean();
        Message.printMsg();
        //交谈
        npc.speak();
        //任务
        if (npc.getTaskMap() != null && npc.getTaskMap().size() > 0) {
            System.out.println("-------------------------任务-------------------------");
            Iterator<Map.Entry<Integer, Task>> iterator = npc.getTaskMap().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Task> next = iterator.next();
                String flag = "";//标记任务状态
                if (!TaskController.isGet(next.getValue())){
                    flag = Format.colorFormat("(可接取任务)",Color.YELLOW);
                }else {
                    if (TaskController.isDone(next.getValue())){
                        flag = Format.colorFormat("(可提交)",Color.GREEN);
                    }else {
                        flag = Format.colorFormat("(未完成)",Color.YELLOW);
                    }
                }
                System.out.println("【查看.look" + next.getKey() + "】"+flag + next.getValue().getName());
            }
            System.out.println("【1.返回界面】【0.返回地图】");
            System.out.print("你的选择:");
            String keyDown = MyScaner.getKeyDown();
            Iterator<Map.Entry<Integer, Task>> iterator1 = npc.getTaskMap().entrySet().iterator();
            while (iterator1.hasNext()) {
                Map.Entry<Integer, Task> next = iterator1.next();
                //选择任务
                if (keyDown.equalsIgnoreCase("LOOK" + next.getKey())) {
                    //播放任务描述]
                    printTask(npc, next.getValue());
                    return;
                }
            }
            switch (keyDown) {
                case "1":
                    printMain();
                    break;
                case "0":
                    printGameMap(MapData.gameMap);
                    break;
                default:
                    printTalk(npc);
            }
        }else {
            System.out.println("【1.交谈】【0.返回地图】");
            System.out.print("你的选择:");
            String keyDown = MyScaner.getKeyDown();
            switch (keyDown){
                case "1":printTalk(npc);break;
                case "0":printGameMap(MapData.gameMap);
                default:printTalk(npc);
            }
        }
    }

    //设置界面
    public static void printSetting() {
        Screen.clean();
        Message.printMsg();
        System.out.println("------------------系统设置------------------");
        System.out.println("【0.返回】");
        System.out.println("【1.保存游戏】");
        System.out.println("【2.保存并退出】");
        System.out.println("-------------------------------------------");
        System.out.print("你的选择:");
        String keyDown = MyScaner.getKeyDown();
        switch (keyDown) {
            case "0":
                printMain();
                break;
            case "1":
                if (Storage.saveGame()) {
                    Message.success("游戏保存成功！！！");
                } else {
                    Message.error("游戏保存失败！！！");
                }
                printSetting();
                break;
            case "2":
                if (Storage.saveGame()) {
                    System.out.println(Format.colorFormat("游戏保存成功！！！", Color.GREEN));
                    System.out.println(Format.colorFormat("3秒后退出游戏...", Color.GREEN));
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.exit(0);
                } else {
                    Message.error("游戏保存失败！！！");
                }
                break;
            default:
                printSetting();
                break;
        }
    }
}
